\documentclass[11pt]{article}

    \usepackage[breakable]{tcolorbox}
    \usepackage{parskip} % Stop auto-indenting (to mimic markdown behaviour)
    
    \usepackage{iftex}
    \ifPDFTeX
    	\usepackage[T1]{fontenc}
    	\usepackage{mathpazo}
    \else
    	\usepackage{fontspec}
    \fi

    % Basic figure setup, for now with no caption control since it's done
    % automatically by Pandoc (which extracts ![](path) syntax from Markdown).
    \usepackage{graphicx}
    % Maintain compatibility with old templates. Remove in nbconvert 6.0
    \let\Oldincludegraphics\includegraphics
    % Ensure that by default, figures have no caption (until we provide a
    % proper Figure object with a Caption API and a way to capture that
    % in the conversion process - todo).
    \usepackage{caption}
    \DeclareCaptionFormat{nocaption}{}
    \captionsetup{format=nocaption,aboveskip=0pt,belowskip=0pt}

    \usepackage{float}
    \floatplacement{figure}{H} % forces figures to be placed at the correct location
    \usepackage{xcolor} % Allow colors to be defined
    \usepackage{enumerate} % Needed for markdown enumerations to work
    \usepackage{geometry} % Used to adjust the document margins
    \usepackage{amsmath} % Equations
    \usepackage{amssymb} % Equations
    \usepackage{textcomp} % defines textquotesingle
    % Hack from http://tex.stackexchange.com/a/47451/13684:
    \AtBeginDocument{%
        \def\PYZsq{\textquotesingle}% Upright quotes in Pygmentized code
    }
    \usepackage{upquote} % Upright quotes for verbatim code
    \usepackage{eurosym} % defines \euro
    \usepackage[mathletters]{ucs} % Extended unicode (utf-8) support
    \usepackage{fancyvrb} % verbatim replacement that allows latex
    \usepackage{grffile} % extends the file name processing of package graphics 
                         % to support a larger range
    \makeatletter % fix for old versions of grffile with XeLaTeX
    \@ifpackagelater{grffile}{2019/11/01}
    {
      % Do nothing on new versions
    }
    {
      \def\Gread@@xetex#1{%
        \IfFileExists{"\Gin@base".bb}%
        {\Gread@eps{\Gin@base.bb}}%
        {\Gread@@xetex@aux#1}%
      }
    }
    \makeatother
    \usepackage[Export]{adjustbox} % Used to constrain images to a maximum size
    \adjustboxset{max size={0.9\linewidth}{0.9\paperheight}}

    % The hyperref package gives us a pdf with properly built
    % internal navigation ('pdf bookmarks' for the table of contents,
    % internal cross-reference links, web links for URLs, etc.)
    \usepackage{hyperref}
    % The default LaTeX title has an obnoxious amount of whitespace. By default,
    % titling removes some of it. It also provides customization options.
    \usepackage{titling}
    \usepackage{longtable} % longtable support required by pandoc >1.10
    \usepackage{booktabs}  % table support for pandoc > 1.12.2
    \usepackage[inline]{enumitem} % IRkernel/repr support (it uses the enumerate* environment)
    \usepackage[normalem]{ulem} % ulem is needed to support strikethroughs (\sout)
                                % normalem makes italics be italics, not underlines
    \usepackage{mathrsfs}
    

    
    % Colors for the hyperref package
    \definecolor{urlcolor}{rgb}{0,.145,.698}
    \definecolor{linkcolor}{rgb}{.71,0.21,0.01}
    \definecolor{citecolor}{rgb}{.12,.54,.11}

    % ANSI colors
    \definecolor{ansi-black}{HTML}{3E424D}
    \definecolor{ansi-black-intense}{HTML}{282C36}
    \definecolor{ansi-red}{HTML}{E75C58}
    \definecolor{ansi-red-intense}{HTML}{B22B31}
    \definecolor{ansi-green}{HTML}{00A250}
    \definecolor{ansi-green-intense}{HTML}{007427}
    \definecolor{ansi-yellow}{HTML}{DDB62B}
    \definecolor{ansi-yellow-intense}{HTML}{B27D12}
    \definecolor{ansi-blue}{HTML}{208FFB}
    \definecolor{ansi-blue-intense}{HTML}{0065CA}
    \definecolor{ansi-magenta}{HTML}{D160C4}
    \definecolor{ansi-magenta-intense}{HTML}{A03196}
    \definecolor{ansi-cyan}{HTML}{60C6C8}
    \definecolor{ansi-cyan-intense}{HTML}{258F8F}
    \definecolor{ansi-white}{HTML}{C5C1B4}
    \definecolor{ansi-white-intense}{HTML}{A1A6B2}
    \definecolor{ansi-default-inverse-fg}{HTML}{FFFFFF}
    \definecolor{ansi-default-inverse-bg}{HTML}{000000}

    % common color for the border for error outputs.
    \definecolor{outerrorbackground}{HTML}{FFDFDF}

    % commands and environments needed by pandoc snippets
    % extracted from the output of `pandoc -s`
    \providecommand{\tightlist}{%
      \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
    \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
    % Add ',fontsize=\small' for more characters per line
    \newenvironment{Shaded}{}{}
    \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
    \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}}
    \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
    \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
    \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
    \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}}
    \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}}
    \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
    \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}}
    \newcommand{\RegionMarkerTok}[1]{{#1}}
    \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
    \newcommand{\NormalTok}[1]{{#1}}
    
    % Additional commands for more recent versions of Pandoc
    \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{{#1}}}
    \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{{#1}}}
    \newcommand{\ImportTok}[1]{{#1}}
    \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{{#1}}}}
    \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{{#1}}}
    \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
    \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{{#1}}}
    \newcommand{\BuiltInTok}[1]{{#1}}
    \newcommand{\ExtensionTok}[1]{{#1}}
    \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{{#1}}}
    \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{{#1}}}
    \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    
    
    % Define a nice break command that doesn't care if a line doesn't already
    % exist.
    \def\br{\hspace*{\fill} \\* }
    % Math Jax compatibility definitions
    \def\gt{>}
    \def\lt{<}
    \let\Oldtex\TeX
    \let\Oldlatex\LaTeX
    \renewcommand{\TeX}{\textrm{\Oldtex}}
    \renewcommand{\LaTeX}{\textrm{\Oldlatex}}
    % Document parameters
    % Document title
    \title{ex\_5\_5}
    
    
    
    
    
% Pygments definitions
\makeatletter
\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%
    \let\PY@ul=\relax \let\PY@tc=\relax%
    \let\PY@bc=\relax \let\PY@ff=\relax}
\def\PY@tok#1{\csname PY@tok@#1\endcsname}
\def\PY@toks#1+{\ifx\relax#1\empty\else%
    \PY@tok{#1}\expandafter\PY@toks\fi}
\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%
    \PY@it{\PY@bf{\PY@ff{#1}}}}}}}
\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}

\@namedef{PY@tok@w}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\@namedef{PY@tok@c}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@cp}{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}}
\@namedef{PY@tok@k}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kp}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kt}{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
\@namedef{PY@tok@o}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@ow}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PY@tok@nb}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@nf}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PY@tok@nc}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PY@tok@nn}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PY@tok@ne}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}}
\@namedef{PY@tok@nv}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@no}{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
\@namedef{PY@tok@nl}{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}}
\@namedef{PY@tok@ni}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}}
\@namedef{PY@tok@na}{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}}
\@namedef{PY@tok@nt}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@nd}{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PY@tok@s}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@sd}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@si}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\@namedef{PY@tok@se}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}}
\@namedef{PY@tok@sr}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\@namedef{PY@tok@ss}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@sx}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@m}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@gh}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PY@tok@gu}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\@namedef{PY@tok@gd}{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\@namedef{PY@tok@gi}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
\@namedef{PY@tok@gr}{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
\@namedef{PY@tok@ge}{\let\PY@it=\textit}
\@namedef{PY@tok@gs}{\let\PY@bf=\textbf}
\@namedef{PY@tok@gp}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PY@tok@go}{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
\@namedef{PY@tok@gt}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
\@namedef{PY@tok@err}{\def\PY@bc##1{{\setlength{\fboxsep}{-\fboxrule}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}}
\@namedef{PY@tok@kc}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kd}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kn}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kr}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@bp}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@fm}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PY@tok@vc}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@vg}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@vi}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@vm}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@sa}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@sb}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@sc}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@dl}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@s2}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@sh}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@s1}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@mb}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@mf}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@mh}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@mi}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@il}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@mo}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@ch}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@cm}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@cpf}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@c1}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@cs}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}

\def\PYZbs{\char`\\}
\def\PYZus{\char`\_}
\def\PYZob{\char`\{}
\def\PYZcb{\char`\}}
\def\PYZca{\char`\^}
\def\PYZam{\char`\&}
\def\PYZlt{\char`\<}
\def\PYZgt{\char`\>}
\def\PYZsh{\char`\#}
\def\PYZpc{\char`\%}
\def\PYZdl{\char`\$}
\def\PYZhy{\char`\-}
\def\PYZsq{\char`\'}
\def\PYZdq{\char`\"}
\def\PYZti{\char`\~}
% for compatibility with earlier versions
\def\PYZat{@}
\def\PYZlb{[}
\def\PYZrb{]}
\makeatother


    % For linebreaks inside Verbatim environment from package fancyvrb. 
    \makeatletter
        \newbox\Wrappedcontinuationbox 
        \newbox\Wrappedvisiblespacebox 
        \newcommand*\Wrappedvisiblespace {\textcolor{red}{\textvisiblespace}} 
        \newcommand*\Wrappedcontinuationsymbol {\textcolor{red}{\llap{\tiny$\m@th\hookrightarrow$}}} 
        \newcommand*\Wrappedcontinuationindent {3ex } 
        \newcommand*\Wrappedafterbreak {\kern\Wrappedcontinuationindent\copy\Wrappedcontinuationbox} 
        % Take advantage of the already applied Pygments mark-up to insert 
        % potential linebreaks for TeX processing. 
        %        {, <, #, %, $, ' and ": go to next line. 
        %        _, }, ^, &, >, - and ~: stay at end of broken line. 
        % Use of \textquotesingle for straight quote. 
        \newcommand*\Wrappedbreaksatspecials {% 
            \def\PYGZus{\discretionary{\char`\_}{\Wrappedafterbreak}{\char`\_}}% 
            \def\PYGZob{\discretionary{}{\Wrappedafterbreak\char`\{}{\char`\{}}% 
            \def\PYGZcb{\discretionary{\char`\}}{\Wrappedafterbreak}{\char`\}}}% 
            \def\PYGZca{\discretionary{\char`\^}{\Wrappedafterbreak}{\char`\^}}% 
            \def\PYGZam{\discretionary{\char`\&}{\Wrappedafterbreak}{\char`\&}}% 
            \def\PYGZlt{\discretionary{}{\Wrappedafterbreak\char`\<}{\char`\<}}% 
            \def\PYGZgt{\discretionary{\char`\>}{\Wrappedafterbreak}{\char`\>}}% 
            \def\PYGZsh{\discretionary{}{\Wrappedafterbreak\char`\#}{\char`\#}}% 
            \def\PYGZpc{\discretionary{}{\Wrappedafterbreak\char`\%}{\char`\%}}% 
            \def\PYGZdl{\discretionary{}{\Wrappedafterbreak\char`\$}{\char`\$}}% 
            \def\PYGZhy{\discretionary{\char`\-}{\Wrappedafterbreak}{\char`\-}}% 
            \def\PYGZsq{\discretionary{}{\Wrappedafterbreak\textquotesingle}{\textquotesingle}}% 
            \def\PYGZdq{\discretionary{}{\Wrappedafterbreak\char`\"}{\char`\"}}% 
            \def\PYGZti{\discretionary{\char`\~}{\Wrappedafterbreak}{\char`\~}}% 
        } 
        % Some characters . , ; ? ! / are not pygmentized. 
        % This macro makes them "active" and they will insert potential linebreaks 
        \newcommand*\Wrappedbreaksatpunct {% 
            \lccode`\~`\.\lowercase{\def~}{\discretionary{\hbox{\char`\.}}{\Wrappedafterbreak}{\hbox{\char`\.}}}% 
            \lccode`\~`\,\lowercase{\def~}{\discretionary{\hbox{\char`\,}}{\Wrappedafterbreak}{\hbox{\char`\,}}}% 
            \lccode`\~`\;\lowercase{\def~}{\discretionary{\hbox{\char`\;}}{\Wrappedafterbreak}{\hbox{\char`\;}}}% 
            \lccode`\~`\:\lowercase{\def~}{\discretionary{\hbox{\char`\:}}{\Wrappedafterbreak}{\hbox{\char`\:}}}% 
            \lccode`\~`\?\lowercase{\def~}{\discretionary{\hbox{\char`\?}}{\Wrappedafterbreak}{\hbox{\char`\?}}}% 
            \lccode`\~`\!\lowercase{\def~}{\discretionary{\hbox{\char`\!}}{\Wrappedafterbreak}{\hbox{\char`\!}}}% 
            \lccode`\~`\/\lowercase{\def~}{\discretionary{\hbox{\char`\/}}{\Wrappedafterbreak}{\hbox{\char`\/}}}% 
            \catcode`\.\active
            \catcode`\,\active 
            \catcode`\;\active
            \catcode`\:\active
            \catcode`\?\active
            \catcode`\!\active
            \catcode`\/\active 
            \lccode`\~`\~ 	
        }
    \makeatother

    \let\OriginalVerbatim=\Verbatim
    \makeatletter
    \renewcommand{\Verbatim}[1][1]{%
        %\parskip\z@skip
        \sbox\Wrappedcontinuationbox {\Wrappedcontinuationsymbol}%
        \sbox\Wrappedvisiblespacebox {\FV@SetupFont\Wrappedvisiblespace}%
        \def\FancyVerbFormatLine ##1{\hsize\linewidth
            \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@
                \doublehyphendemerits\z@\finalhyphendemerits\z@
                \strut ##1\strut}%
        }%
        % If the linebreak is at a space, the latter will be displayed as visible
        % space at end of first line, and a continuation symbol starts next line.
        % Stretch/shrink are however usually zero for typewriter font.
        \def\FV@Space {%
            \nobreak\hskip\z@ plus\fontdimen3\font minus\fontdimen4\font
            \discretionary{\copy\Wrappedvisiblespacebox}{\Wrappedafterbreak}
            {\kern\fontdimen2\font}%
        }%
        
        % Allow breaks at special characters using \PYG... macros.
        \Wrappedbreaksatspecials
        % Breaks at punctuation characters . , ; ? ! and / need catcode=\active 	
        \OriginalVerbatim[#1,codes*=\Wrappedbreaksatpunct]%
    }
    \makeatother

    % Exact colors from NB
    \definecolor{incolor}{HTML}{303F9F}
    \definecolor{outcolor}{HTML}{D84315}
    \definecolor{cellborder}{HTML}{CFCFCF}
    \definecolor{cellbackground}{HTML}{F7F7F7}
    
    % prompt
    \makeatletter
    \newcommand{\boxspacing}{\kern\kvtcb@left@rule\kern\kvtcb@boxsep}
    \makeatother
    \newcommand{\prompt}[4]{
        {\ttfamily\llap{{\color{#2}[#3]:\hspace{3pt}#4}}\vspace{-\baselineskip}}
    }
    

    
    % Prevent overflowing lines due to hard-to-break entities
    \sloppy 
    % Setup hyperref package
    \hypersetup{
      breaklinks=true,  % so long urls are correctly broken across lines
      colorlinks=true,
      urlcolor=urlcolor,
      linkcolor=linkcolor,
      citecolor=citecolor,
      }
    % Slightly bigger margins than the latex defaults
    
    \geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
    
    

\begin{document}
    
    \maketitle
    
    

    
    \hypertarget{ux4e60ux9898-5.5}{%
\section{习题 5.5}\label{ux4e60ux9898-5.5}}

\includegraphics{https://tva1.sinaimg.cn/large/008i3skNly1grc30ihsyjj31l60dcq89.jpg}
\includegraphics{https://tva1.sinaimg.cn/large/008i3skNly1grc32f19fqj31760fgtej.jpg}

    鸢尾花（iris）分类是常用数据集，在 R 有内置：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{1}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{data}\PY{p}{(}\PY{n}{iris}\PY{p}{)}
\PY{n+nf}{attach}\PY{p}{(}\PY{n}{iris}\PY{p}{)}
\PY{n}{iris}
\end{Verbatim}
\end{tcolorbox}

    A data.frame: 150 × 5
\begin{tabular}{lllll}
 Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
 <dbl> & <dbl> & <dbl> & <dbl> & <fct>\\
\hline
	 5.1 & 3.5 & 1.4 & 0.2 & setosa\\
	 4.9 & 3.0 & 1.4 & 0.2 & setosa\\
	 4.7 & 3.2 & 1.3 & 0.2 & setosa\\
	 4.6 & 3.1 & 1.5 & 0.2 & setosa\\
	 5.0 & 3.6 & 1.4 & 0.2 & setosa\\
	 5.4 & 3.9 & 1.7 & 0.4 & setosa\\
	 4.6 & 3.4 & 1.4 & 0.3 & setosa\\
	 5.0 & 3.4 & 1.5 & 0.2 & setosa\\
	 4.4 & 2.9 & 1.4 & 0.2 & setosa\\
	 4.9 & 3.1 & 1.5 & 0.1 & setosa\\
	 5.4 & 3.7 & 1.5 & 0.2 & setosa\\
	 4.8 & 3.4 & 1.6 & 0.2 & setosa\\
	 4.8 & 3.0 & 1.4 & 0.1 & setosa\\
	 4.3 & 3.0 & 1.1 & 0.1 & setosa\\
	 5.8 & 4.0 & 1.2 & 0.2 & setosa\\
	 5.7 & 4.4 & 1.5 & 0.4 & setosa\\
	 5.4 & 3.9 & 1.3 & 0.4 & setosa\\
	 5.1 & 3.5 & 1.4 & 0.3 & setosa\\
	 5.7 & 3.8 & 1.7 & 0.3 & setosa\\
	 5.1 & 3.8 & 1.5 & 0.3 & setosa\\
	 5.4 & 3.4 & 1.7 & 0.2 & setosa\\
	 5.1 & 3.7 & 1.5 & 0.4 & setosa\\
	 4.6 & 3.6 & 1.0 & 0.2 & setosa\\
	 5.1 & 3.3 & 1.7 & 0.5 & setosa\\
	 4.8 & 3.4 & 1.9 & 0.2 & setosa\\
	 5.0 & 3.0 & 1.6 & 0.2 & setosa\\
	 5.0 & 3.4 & 1.6 & 0.4 & setosa\\
	 5.2 & 3.5 & 1.5 & 0.2 & setosa\\
	 5.2 & 3.4 & 1.4 & 0.2 & setosa\\
	 4.7 & 3.2 & 1.6 & 0.2 & setosa\\
	 ⋮ & ⋮ & ⋮ & ⋮ & ⋮\\
	 6.9 & 3.2 & 5.7 & 2.3 & virginica\\
	 5.6 & 2.8 & 4.9 & 2.0 & virginica\\
	 7.7 & 2.8 & 6.7 & 2.0 & virginica\\
	 6.3 & 2.7 & 4.9 & 1.8 & virginica\\
	 6.7 & 3.3 & 5.7 & 2.1 & virginica\\
	 7.2 & 3.2 & 6.0 & 1.8 & virginica\\
	 6.2 & 2.8 & 4.8 & 1.8 & virginica\\
	 6.1 & 3.0 & 4.9 & 1.8 & virginica\\
	 6.4 & 2.8 & 5.6 & 2.1 & virginica\\
	 7.2 & 3.0 & 5.8 & 1.6 & virginica\\
	 7.4 & 2.8 & 6.1 & 1.9 & virginica\\
	 7.9 & 3.8 & 6.4 & 2.0 & virginica\\
	 6.4 & 2.8 & 5.6 & 2.2 & virginica\\
	 6.3 & 2.8 & 5.1 & 1.5 & virginica\\
	 6.1 & 2.6 & 5.6 & 1.4 & virginica\\
	 7.7 & 3.0 & 6.1 & 2.3 & virginica\\
	 6.3 & 3.4 & 5.6 & 2.4 & virginica\\
	 6.4 & 3.1 & 5.5 & 1.8 & virginica\\
	 6.0 & 3.0 & 4.8 & 1.8 & virginica\\
	 6.9 & 3.1 & 5.4 & 2.1 & virginica\\
	 6.7 & 3.1 & 5.6 & 2.4 & virginica\\
	 6.9 & 3.1 & 5.1 & 2.3 & virginica\\
	 5.8 & 2.7 & 5.1 & 1.9 & virginica\\
	 6.8 & 3.2 & 5.9 & 2.3 & virginica\\
	 6.7 & 3.3 & 5.7 & 2.5 & virginica\\
	 6.7 & 3.0 & 5.2 & 2.3 & virginica\\
	 6.3 & 2.5 & 5.0 & 1.9 & virginica\\
	 6.5 & 3.0 & 5.2 & 2.0 & virginica\\
	 6.2 & 3.4 & 5.4 & 2.3 & virginica\\
	 5.9 & 3.0 & 5.1 & 1.8 & virginica\\
\end{tabular}


    
    这些数据形状与书上题目所给数据一直，只是 \(x_i\) 值相差常数倍（10x）。
为保证这个数据集确是和书上一致，下面随意验证几个：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{2}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{iris}\PY{p}{[}\PY{n}{Sepal.Length}\PY{o}{==}\PY{l+m}{5.0} \PY{o}{\PYZam{}} \PY{n}{Sepal.Width}\PY{o}{==}\PY{l+m}{3.3}\PY{p}{,} \PY{p}{]}    \PY{c+c1}{\PYZsh{} 书上的 1}
\end{Verbatim}
\end{tcolorbox}

    A data.frame: 1 × 5
\begin{tabular}{r|lllll}
  & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
  & <dbl> & <dbl> & <dbl> & <dbl> & <fct>\\
\hline
	50 & 5 & 3.3 & 1.4 & 0.2 & setosa\\
\end{tabular}


    
    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{3}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{iris}\PY{p}{[}\PY{n}{Sepal.Length}\PY{o}{==}\PY{l+m}{6.5} \PY{o}{\PYZam{}} \PY{n}{Sepal.Width}\PY{o}{==}\PY{l+m}{2.8}\PY{p}{,} \PY{p}{]}    \PY{c+c1}{\PYZsh{} 书上的 51}
\end{Verbatim}
\end{tcolorbox}

    A data.frame: 1 × 5
\begin{tabular}{r|lllll}
  & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
  & <dbl> & <dbl> & <dbl> & <dbl> & <fct>\\
\hline
	55 & 6.5 & 2.8 & 4.6 & 1.5 & versicolor\\
\end{tabular}


    
    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{4}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{iris}\PY{p}{[}\PY{n}{Sepal.Length}\PY{o}{==}\PY{l+m}{6.4} \PY{o}{\PYZam{}} \PY{n}{Sepal.Width}\PY{o}{==}\PY{l+m}{2.8}\PY{p}{,} \PY{p}{]}    \PY{c+c1}{\PYZsh{} 书上的 125、101}
\end{Verbatim}
\end{tcolorbox}

    A data.frame: 2 × 5
\begin{tabular}{r|lllll}
  & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
  & <dbl> & <dbl> & <dbl> & <dbl> & <fct>\\
\hline
	129 & 6.4 & 2.8 & 5.6 & 2.1 & virginica\\
	133 & 6.4 & 2.8 & 5.6 & 2.2 & virginica\\
\end{tabular}


    
    可以把数据放大 10 倍，得到书上的数据（这里不这么做）：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{5}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{c+c1}{\PYZsh{} iris[\PYZhy{}5] \PYZlt{}\PYZhy{} iris[\PYZhy{}5] * 10; iris}
\end{Verbatim}
\end{tcolorbox}

    \hypertarget{section}{%
\subsection{(2)}\label{section}}

先做\textbf{总体协方差矩阵相同}的情况：

只考虑 \(X_2\)、\(X_4\) 即 \texttt{Sepal.Width} 和 \texttt{Petal.Width}:

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{6}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{library}\PY{p}{(}\PY{n}{MASS}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{7}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{l1} \PY{o}{=} \PY{n+nf}{lda}\PY{p}{(}\PY{n}{Species} \PY{o}{\PYZti{}} \PY{n}{Sepal.Width} \PY{o}{+} \PY{n}{Petal.Width}\PY{p}{,} \PY{n}{data}\PY{o}{=}\PY{n}{iris}\PY{p}{)}\PY{p}{;} \PY{n}{l1}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
Call:
lda(Species \textasciitilde{} Sepal.Width + Petal.Width, data = iris)

Prior probabilities of groups:
    setosa versicolor  virginica 
 0.3333333  0.3333333  0.3333333 

Group means:
           Sepal.Width Petal.Width
setosa           3.428       0.246
versicolor       2.770       1.326
virginica        2.974       2.026

Coefficients of linear discriminants:
                  LD1       LD2
Sepal.Width -1.986964 2.6800746
Petal.Width  5.477136 0.8169648

Proportion of trace:
   LD1    LD2 
0.9884 0.0116 
    \end{Verbatim}

    
    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{8}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{l1}\PY{o}{\PYZdl{}}\PY{n}{scaling}
\end{Verbatim}
\end{tcolorbox}

    A matrix: 2 × 2 of type dbl
\begin{tabular}{r|ll}
  & LD1 & LD2\\
\hline
	Sepal.Width & -1.986964 & 2.6800746\\
	Petal.Width &  5.477136 & 0.8169648\\
\end{tabular}


    
    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{9}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{c+c1}{\PYZsh{} 输出线性判别函数}
\PY{n+nf}{cat}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{W\PYZus{}1 =\PYZdq{}}\PY{p}{,} \PY{n+nf}{paste}\PY{p}{(}\PY{n+nf}{round}\PY{p}{(}\PY{n}{l1}\PY{o}{\PYZdl{}}\PY{n}{scaling}\PY{p}{[}\PY{l+m}{1}\PY{o}{:}\PY{l+m}{2}\PY{p}{]}\PY{p}{,} \PY{l+m}{8}\PY{p}{)}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{*x\PYZus{}\PYZdq{}}\PY{p}{,} \PY{l+m}{1}\PY{o}{:}\PY{l+m}{2}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{ +\PYZdq{}}\PY{p}{,} \PY{n}{sep}\PY{o}{=}\PY{l+s}{\PYZdq{}}\PY{l+s}{\PYZdq{}}\PY{p}{)}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{\PYZbs{}n\PYZsq{}}\PY{p}{)}
\PY{n+nf}{cat}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{W\PYZus{}2 =\PYZdq{}}\PY{p}{,} \PY{n+nf}{paste}\PY{p}{(}\PY{n+nf}{round}\PY{p}{(}\PY{n}{l1}\PY{o}{\PYZdl{}}\PY{n}{scaling}\PY{p}{[}\PY{l+m}{3}\PY{o}{:}\PY{l+m}{4}\PY{p}{]}\PY{p}{,} \PY{l+m}{8}\PY{p}{)}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{*x\PYZus{}\PYZdq{}}\PY{p}{,} \PY{l+m}{1}\PY{o}{:}\PY{l+m}{2}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{ +\PYZdq{}}\PY{p}{,} \PY{n}{sep}\PY{o}{=}\PY{l+s}{\PYZdq{}}\PY{l+s}{\PYZdq{}}\PY{p}{)}\PY{p}{,} \PY{l+s}{\PYZsq{}}\PY{l+s}{\PYZbs{}n\PYZsq{}}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{Verbatim}[commandchars=\\\{\}]
W\_1 = -1.98696447*x\_1 + 5.47713646*x\_2 +
W\_2 = 2.6800746*x\_1 + 0.81696482*x\_2 +
    \end{Verbatim}

    得到线性判别函数：

\[
\begin{aligned}
W_1 &= -1.98696447 x_1 + 5.47713646 x_2 \\
W_2 &= 2.6800746 x_1 + 0.81696482 x_2 
\end{aligned}
\]

    为方便进行误判确认，封装一个工具函数：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{10}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{prederr} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{function}\PY{p}{(}\PY{n}{pred}\PY{p}{,} \PY{n}{true.group}\PY{p}{)} \PY{p}{\PYZob{}}
    \PY{c+c1}{\PYZsh{} lda 判别分析预测误判}
    \PY{c+c1}{\PYZsh{} Args:}
    \PY{c+c1}{\PYZsh{}    pred: lda 的预测, e.g. predict(ldm(Species \PYZti{} ., data = data))}
    \PY{c+c1}{\PYZsh{}    true.group: 实际的分类（data 的 label）}
    \PY{c+c1}{\PYZsh{} Returns:}
    \PY{c+c1}{\PYZsh{}    list(`tab`, `err`, `err.ratio`)}
    \PY{c+c1}{\PYZsh{}      tab: 实际与预判交叉表}
    \PY{c+c1}{\PYZsh{}      err: 错误分类数据列表}
    \PY{c+c1}{\PYZsh{}      err.ratio: 误判率}
    
    \PY{n}{tab} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{table}\PY{p}{(}\PY{n}{Species}\PY{p}{,} \PY{n}{pred}\PY{o}{\PYZdl{}}\PY{n}{class}\PY{p}{)}
    \PY{n}{err} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{data.frame}\PY{p}{(}\PY{n}{pred}\PY{p}{,} \PY{n}{true.group}\PY{p}{)}\PY{p}{[}\PY{n}{pred}\PY{o}{\PYZdl{}}\PY{n}{class} \PY{o}{!=} \PY{n}{true.group}\PY{p}{,}\PY{p}{]}
    \PY{n}{err.ratio} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{length}\PY{p}{(}\PY{n}{err}\PY{o}{\PYZdl{}}\PY{n}{class}\PY{p}{)} \PY{o}{/} \PY{n+nf}{length}\PY{p}{(}\PY{n}{pred}\PY{o}{\PYZdl{}}\PY{n}{class}\PY{p}{)}
    
    \PY{c+c1}{\PYZsh{} ret}
    \PY{n+nf}{list}\PY{p}{(}\PY{n}{`tab`}\PY{o}{=}\PY{n}{tab}\PY{p}{,} \PY{n}{`err`}\PY{o}{=}\PY{n}{err}\PY{p}{,} \PY{n}{`err.ratio`}\PY{o}{=}\PY{n}{err.ratio}\PY{p}{)}
\PY{p}{\PYZcb{}}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{11}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{pred1} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{predict}\PY{p}{(}\PY{n}{l1}\PY{p}{)}
\PY{n+nf}{prederr}\PY{p}{(}\PY{n}{pred1}\PY{p}{,} \PY{n}{Species}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
\$tab
            
Species      setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         49         1
  virginica       0          4        46

\$err
         class posterior.setosa posterior.versicolor posterior.virginica
71   virginica     9.920568e-18            0.2331276          0.76687239
120 versicolor     1.919451e-19            0.7114697          0.28853027
130 versicolor     3.125859e-15            0.8792078          0.12079219
134 versicolor     5.868316e-15            0.9605066          0.03949344
135 versicolor     1.037139e-14            0.9878440          0.01215599
       x.LD1      x.LD2 true.group
71  3.006460  0.8730808 versicolor
120 3.350283 -2.0520832  virginica
130 2.308425  0.1736730  virginica
134 2.158105 -0.4440384  virginica
135 2.007784 -1.0617498  virginica

\$err.ratio
[1] 0.03333333

    \end{Verbatim}

    
    得到误判率 \(3.33\%\).

    交叉确认误判率：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{12}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{l1.cv} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{lda}\PY{p}{(}\PY{n}{Species} \PY{o}{\PYZti{}} \PY{n}{Sepal.Width} \PY{o}{+} \PY{n}{Petal.Width}\PY{p}{,} \PY{n}{data}\PY{o}{=}\PY{n}{iris}\PY{p}{,} \PY{n}{CV}\PY{o}{=}\PY{k+kc}{TRUE}\PY{p}{)}
\PY{n+nf}{prederr}\PY{p}{(}\PY{n+nf}{data.frame}\PY{p}{(}\PY{n}{class}\PY{o}{=}\PY{n}{l1.cv}\PY{o}{\PYZdl{}}\PY{n}{class}\PY{p}{,} \PY{n}{l1.cv}\PY{o}{\PYZdl{}}\PY{n}{posterior}\PY{p}{)}\PY{p}{,} \PY{n}{Species}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
\$tab
            
Species      setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         48         2
  virginica       0          4        46

\$err
         class       setosa versicolor   virginica true.group
71   virginica 5.100762e-18  0.2004581 0.799541885 versicolor
78   virginica 2.536707e-17  0.4845668 0.515433247 versicolor
120 versicolor 2.400102e-19  0.7871770 0.212822971  virginica
130 versicolor 1.021173e-15  0.8990896 0.100910442  virginica
134 versicolor 1.776898e-15  0.9704931 0.029506851  virginica
135 versicolor 3.229543e-15  0.9926251 0.007374932  virginica

\$err.ratio
[1] 0.04

    \end{Verbatim}

    
    得到误判率 \(4\%\).

    \hypertarget{section}{%
\subsection{(1)}\label{section}}

总体协方差矩阵不相同的情况，用 \texttt{qda} 来做：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{13}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{qd} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{qda}\PY{p}{(}\PY{n}{Species} \PY{o}{\PYZti{}} \PY{n}{Sepal.Width} \PY{o}{+} \PY{n}{Petal.Width}\PY{p}{,} \PY{n}{data}\PY{o}{=}\PY{n}{iris}\PY{p}{)}\PY{p}{;} \PY{n}{qd}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
Call:
qda(Species \textasciitilde{} Sepal.Width + Petal.Width, data = iris)

Prior probabilities of groups:
    setosa versicolor  virginica 
 0.3333333  0.3333333  0.3333333 

Group means:
           Sepal.Width Petal.Width
setosa           3.428       0.246
versicolor       2.770       1.326
virginica        2.974       2.026
    \end{Verbatim}

    
    预测，并查看误判情况：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{14}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{predq} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{predict}\PY{p}{(}\PY{n}{qd}\PY{p}{)}
\PY{n+nf}{prederr}\PY{p}{(}\PY{n}{predq}\PY{p}{,} \PY{n}{Species}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
\$tab
            
Species      setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         46         4
  virginica       0          3        47

\$err
         class posterior.setosa posterior.versicolor posterior.virginica
69   virginica     3.612396e-38            0.1288885          0.87111146
71   virginica     5.782918e-51            0.2345794          0.76542060
78   virginica     7.578016e-46            0.3801197          0.61988031
84   virginica     1.621740e-41            0.3894875          0.61051249
130 versicolor     4.565572e-40            0.7817783          0.21822173
134 versicolor     1.077297e-35            0.8674122          0.13258783
135 versicolor     1.359084e-31            0.9204160          0.07958403
    true.group
69  versicolor
71  versicolor
78  versicolor
84  versicolor
130  virginica
134  virginica
135  virginica

\$err.ratio
[1] 0.04666667

    \end{Verbatim}

    
    得到误判率 \(4.67\%\).

    交叉确认误判率：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{15}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{qd.cv} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{qda}\PY{p}{(}\PY{n}{Species} \PY{o}{\PYZti{}} \PY{n}{Sepal.Width} \PY{o}{+} \PY{n}{Petal.Width}\PY{p}{,} \PY{n}{data}\PY{o}{=}\PY{n}{iris}\PY{p}{,} \PY{n}{CV}\PY{o}{=}\PY{k+kc}{TRUE}\PY{p}{)}
\PY{n+nf}{prederr}\PY{p}{(}\PY{n+nf}{data.frame}\PY{p}{(}\PY{n}{class}\PY{o}{=}\PY{n}{qd.cv}\PY{o}{\PYZdl{}}\PY{n}{class}\PY{p}{,} \PY{n}{qd.cv}\PY{o}{\PYZdl{}}\PY{n}{posterior}\PY{p}{)}\PY{p}{,} \PY{n}{Species}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
\$tab
            
Species      setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         45         5
  virginica       0          3        47

\$err
         class       setosa versicolor  virginica true.group
69   virginica 4.037306e-38 0.02642376 0.97357624 versicolor
71   virginica 6.294572e-51 0.16685743 0.83314257 versicolor
73   virginica 7.169125e-37 0.48449460 0.51550540 versicolor
78   virginica 8.162732e-46 0.33228997 0.66771003 versicolor
84   virginica 1.761570e-41 0.33684744 0.66315256 versicolor
130 versicolor 4.722285e-40 0.80861274 0.19138726  virginica
134 versicolor 1.104200e-35 0.88907345 0.11092655  virginica
135 versicolor 1.390787e-31 0.94188622 0.05811378  virginica

\$err.ratio
[1] 0.05333333

    \end{Verbatim}

    
    得到误判率 \(5.33\%\).

    \hypertarget{section}{%
\subsection{（3）}\label{section}}

新样本：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{16}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{x0} \PY{o}{=} \PY{n+nf}{data.frame}\PY{p}{(}\PY{n}{Sepal.Width} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{35}\PY{p}{)}\PY{p}{,} \PY{n}{Petal.Width}\PY{o}{=}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{18}\PY{p}{)}\PY{p}{)}\PY{p}{;} \PY{n}{x0}
\end{Verbatim}
\end{tcolorbox}

    A data.frame: 1 × 2
\begin{tabular}{ll}
 Sepal.Width & Petal.Width\\
 <dbl> & <dbl>\\
\hline
	 35 & 18\\
\end{tabular}


    
    带入 lda 模型：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{17}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{predict}\PY{p}{(}\PY{n}{l1}\PY{p}{,} \PY{n}{newdata}\PY{o}{=}\PY{n}{x0}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{description}
\item[\$class] virginica
\emph{Levels}: \begin{enumerate*}
\item 'setosa'
\item 'versicolor'
\item 'virginica'
\end{enumerate*}

\item[\$posterior] A matrix: 1 × 3 of type dbl
\begin{tabular}{r|lll}
  & setosa & versicolor & virginica\\
\hline
	1 & 6.24321e-146 & 4.105739e-87 & 1\\
\end{tabular}

\item[\$x] A matrix: 1 × 2 of type dbl
\begin{tabular}{r|ll}
  & LD1 & LD2\\
\hline
	1 & 28.5506 & 99.33428\\
\end{tabular}

\end{description}


    
    判为 \texttt{virginica} 类型，即品种 3。

    带入 qda 模型：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{18}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{predict}\PY{p}{(}\PY{n}{qd}\PY{p}{,} \PY{n}{newdata}\PY{o}{=}\PY{n}{x0}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{description}
\item[\$class] virginica
\emph{Levels}: \begin{enumerate*}
\item 'setosa'
\item 'versicolor'
\item 'virginica'
\end{enumerate*}

\item[\$posterior] A matrix: 1 × 3 of type dbl
\begin{tabular}{r|lll}
  & setosa & versicolor & virginica\\
\hline
	1 & 0 & 9.004664e-244 & 1\\
\end{tabular}

\end{description}


    
    两种模型给出了相同的预测结果。

    \hypertarget{section}{%
\subsection{(4)}\label{section}}

使用全部指标。

\hypertarget{lda}{%
\subsubsection{lda}\label{lda}}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{19}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{l2} \PY{o}{=} \PY{n+nf}{lda}\PY{p}{(}\PY{n}{Species} \PY{o}{\PYZti{}} \PY{n}{.}\PY{p}{,} \PY{n}{data}\PY{o}{=}\PY{n}{iris}\PY{p}{)}\PY{p}{;} \PY{n}{l2}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
Call:
lda(Species \textasciitilde{} ., data = iris)

Prior probabilities of groups:
    setosa versicolor  virginica 
 0.3333333  0.3333333  0.3333333 

Group means:
           Sepal.Length Sepal.Width Petal.Length Petal.Width
setosa            5.006       3.428        1.462       0.246
versicolor        5.936       2.770        4.260       1.326
virginica         6.588       2.974        5.552       2.026

Coefficients of linear discriminants:
                    LD1         LD2
Sepal.Length  0.8293776  0.02410215
Sepal.Width   1.5344731  2.16452123
Petal.Length -2.2012117 -0.93192121
Petal.Width  -2.8104603  2.83918785

Proportion of trace:
   LD1    LD2 
0.9912 0.0088 
    \end{Verbatim}

    
    误判：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{20}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{pred2} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{predict}\PY{p}{(}\PY{n}{l2}\PY{p}{)}
\PY{n+nf}{prederr}\PY{p}{(}\PY{n}{pred2}\PY{p}{,} \PY{n}{Species}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
\$tab
            
Species      setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         48         2
  virginica       0          1        49

\$err
         class posterior.setosa posterior.versicolor posterior.virginica
71   virginica     7.408118e-28            0.2532282           0.7467718
84   virginica     4.241952e-32            0.1433919           0.8566081
134 versicolor     1.283891e-28            0.7293881           0.2706119
        x.LD1      x.LD2 true.group
71  -3.715896  1.0445144 versicolor
84  -4.498466 -0.8827499 versicolor
134 -3.815160 -0.9429859  virginica

\$err.ratio
[1] 0.02

    \end{Verbatim}

    
    交叉确认误判率：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{21}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{l2.cv} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{lda}\PY{p}{(}\PY{n}{Species} \PY{o}{\PYZti{}} \PY{n}{.}\PY{p}{,} \PY{n}{data}\PY{o}{=}\PY{n}{iris}\PY{p}{,} \PY{n}{CV}\PY{o}{=}\PY{k+kc}{TRUE}\PY{p}{)}
\PY{n+nf}{prederr}\PY{p}{(}\PY{n+nf}{data.frame}\PY{p}{(}\PY{n}{class}\PY{o}{=}\PY{n}{l2.cv}\PY{o}{\PYZdl{}}\PY{n}{class}\PY{p}{,} \PY{n}{l2.cv}\PY{o}{\PYZdl{}}\PY{n}{posterior}\PY{p}{)}\PY{p}{,} \PY{n}{Species}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
\$tab
            
Species      setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         48         2
  virginica       0          1        49

\$err
         class       setosa versicolor virginica true.group
71   virginica 1.302246e-28 0.17727267 0.8227273 versicolor
84   virginica 1.125494e-33 0.09924153 0.9007585 versicolor
134 versicolor 5.464475e-29 0.78762376 0.2123762  virginica

\$err.ratio
[1] 0.02

    \end{Verbatim}

    
    误判率比只用两个指标明显提升了。

    \hypertarget{qda}{%
\subsubsection{qda}\label{qda}}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{22}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{qd2} \PY{o}{=} \PY{n+nf}{qda}\PY{p}{(}\PY{n}{Species} \PY{o}{\PYZti{}} \PY{n}{.}\PY{p}{,} \PY{n}{data}\PY{o}{=}\PY{n}{iris}\PY{p}{)}\PY{p}{;} \PY{n}{qd2}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
Call:
qda(Species \textasciitilde{} ., data = iris)

Prior probabilities of groups:
    setosa versicolor  virginica 
 0.3333333  0.3333333  0.3333333 

Group means:
           Sepal.Length Sepal.Width Petal.Length Petal.Width
setosa            5.006       3.428        1.462       0.246
versicolor        5.936       2.770        4.260       1.326
virginica         6.588       2.974        5.552       2.026
    \end{Verbatim}

    
    误判：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{23}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{predq2} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{predict}\PY{p}{(}\PY{n}{qd2}\PY{p}{)}
\PY{n+nf}{prederr}\PY{p}{(}\PY{n}{predq2}\PY{p}{,} \PY{n}{Species}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
\$tab
            
Species      setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         48         2
  virginica       0          1        49

\$err
         class posterior.setosa posterior.versicolor posterior.virginica
71   virginica    1.052723e-103            0.3359442           0.6640558
84   virginica    4.102009e-114            0.1543483           0.8456517
134 versicolor    4.550670e-111            0.6049611           0.3950389
    true.group
71  versicolor
84  versicolor
134  virginica

\$err.ratio
[1] 0.02

    \end{Verbatim}

    
    交叉确认误判率：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{24}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{qd2.cv} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{qda}\PY{p}{(}\PY{n}{Species} \PY{o}{\PYZti{}} \PY{n}{.}\PY{p}{,} \PY{n}{data}\PY{o}{=}\PY{n}{iris}\PY{p}{,} \PY{n}{CV}\PY{o}{=}\PY{k+kc}{TRUE}\PY{p}{)}
\PY{n+nf}{prederr}\PY{p}{(}\PY{n+nf}{data.frame}\PY{p}{(}\PY{n}{class}\PY{o}{=}\PY{n}{qd2.cv}\PY{o}{\PYZdl{}}\PY{n}{class}\PY{p}{,} \PY{n}{qd2.cv}\PY{o}{\PYZdl{}}\PY{n}{posterior}\PY{p}{)}\PY{p}{,} \PY{n}{Species}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
\$tab
            
Species      setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         47         3
  virginica       0          1        49

\$err
         class        setosa versicolor virginica true.group
69   virginica  1.376175e-89 0.31342177 0.6865782 versicolor
71   virginica 1.329043e-103 0.16164225 0.8383577 versicolor
84   virginica 4.504693e-114 0.07133282 0.9286672 versicolor
134 versicolor 4.988739e-111 0.66319758 0.3368024  virginica

\$err.ratio
[1] 0.02666667

    \end{Verbatim}

    
    同样，比只使用两个特征做出来的效果更好，误判率更低。


    % Add a bibliography block to the postdoc
    
    
    
\end{document}
